package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.*;
import com.itheima.entity.vo.SetmealVo;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishService dishService;
    @Autowired
    private CategoryDao categoryDao;


    @Override
    public IPage<SetmealVo> getAll(String name, Integer page,
                                Integer pageSize) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        lqw.like(name !=null,Setmeal::getName,name);

        IPage<Setmeal> iPage = new Page<>();

        iPage.setPages(page).setPages(pageSize);

        IPage<Setmeal> setmealPage = setmealDao.selectPage(iPage, lqw);

        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealVo> setmealVoList = records.stream().map((res) -> {
            SetmealVo sv = new SetmealVo();
            Long id = res.getCategoryId();
            Category category = categoryDao.selectById(id);

            sv.setCategoryName(category.getName());
            BeanUtils.copyProperties(res, sv);
            return sv;
        }).collect(Collectors.toList());

        IPage<SetmealVo> voIPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,voIPage);
        voIPage.setRecords(setmealVoList);



        return voIPage;

    }




    @Override
    public boolean add(SetmealVo setmealVo) {
        try {
            setmealVo.setCreateTime(LocalDateTime.now());
            setmealVo.setUpdateTime(LocalDateTime.now());
            setmealDao.insert(setmealVo); //添加套餐

            List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
            SetmealDish setmealDish = new SetmealDish();

            for (SetmealDish setmeal : setmealDishes) {
                setmeal.setSetmealId(setmealVo.getId()); //获取套餐id
                setmeal.setCreateTime(setmealVo.getCreateTime());
                setmeal.setUpdateTime(setmealVo.getUpdateTime());
                setmeal.setCreateUser(setmealVo.getCreateUser());
                setmeal.setUpdateUser(setmealVo.getUpdateUser());
                dishService.add(setmeal);

            }
        } catch (Exception e) {
            return  false;
        }
        return true;

    }

    @Override
    public SetmealVo updateById(Long id) {

        Setmeal setmeal = setmealDao.selectById(id); //id查询套餐

        List<SetmealDish> setmealDishes = dishService.updateById(id);//id 查询套餐菜品关系表

         SetmealVo setmealVo = new SetmealVo();

         BeanUtils.copyProperties(setmeal,setmealVo);

         setmealVo.setSetmealDishes(setmealDishes);


        return setmealVo;
    }

    @Override
    public boolean update(SetmealVo setmealVo) {
        //TODO 这里修改有点问题
//        try {
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealVo,setmeal);
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getId,setmeal.getId());

            setmealDao.update(setmeal,lqw);

            List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
            dishService.delete(setmealVo.getId());
        for (SetmealDish setmealDish : setmealDishes) {
            System.out.println(setmealDish);
            setmealDish.setCreateTime(setmealVo.getCreateTime());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealVo.getCreateUser());
            setmealDish.setUpdateUser(setmealVo.getUpdateUser());
            setmealDish.setSetmealId(setmealVo.getId());
              dishService.add(setmealDish);
         }


        return true;
    }

    @Override
    public boolean updateStatus(Integer status, List<Long> ids) {
        try {
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.in(Setmeal::getId,ids);
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmealDao.update(setmeal,lqw);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public boolean delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        lqw.in(Setmeal::getId,ids);



        return   setmealDao.delete(lqw) == 0 ? false : true;
    }

    @Override
    public List<Setmeal> getByCategoryId(Long id,String name) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id!=null, Setmeal::getCategoryId, id);
        lqw.like(name!=null,Setmeal::getName,name);

        return setmealDao.selectList(lqw);
    }
}
